package com.example.my.first.app;

import java.io.IOException;
import java.net.MalformedURLException;

import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
	
	private Button registerb =null;
	public final static String App_Id = "352784094815429";
	Facebook fb;
	ImageView image;
	ImageButton imageButton;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    	fb = new Facebook(App_Id);
        imageButton = (ImageButton)findViewById(R.id.imageButton);
        image = (ImageView)findViewById(R.id.image);
        imageButton.setOnClickListener(this);
        updateButtonImage();
        registerb = (Button)findViewById(R.id.registerb);
        registerb.setOnClickListener(MyBListener);
    }

    private OnClickListener MyBListener = new OnClickListener(){

		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent=new Intent();
			intent.setClass(MainActivity.this, RegistrationActivty.class);
			MainActivity.this.startActivity(intent);
		}
   };
    public void updateButtonImage() {
		if(fb.isSessionValid()){
			imageButton.setImageResource(R.drawable.logout);
		}
		else{
			imageButton.setImageResource(R.drawable.login);
		}
	}
    

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(fb.isSessionValid()){
			//button log out facebook
			try {
				fb.logout(getApplicationContext());
				updateButtonImage();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else{
			//log in
			fb.authorize(MainActivity.this, new DialogListener() {
				
				public void onFacebookError(FacebookError e) {
					// TODO Auto-generated method stub
					Toast.makeText(MainActivity.this, "onFaceBook", Toast.LENGTH_LONG).show();
					
				}
				
				public void onError(DialogError e) {
					// TODO Auto-generated method stub
					Toast.makeText(MainActivity.this, "onError", Toast.LENGTH_LONG).show();
					
				}
				
				public void onComplete(Bundle values) {
					// TODO Auto-generated method stub
					updateButtonImage();
					
				}
				
				public void onCancel() {
					// TODO Auto-generated method stub
					Toast.makeText(MainActivity.this, "onCancel", Toast.LENGTH_LONG).show();
					
				}
			});
		}
		
	}
	
}
